package com.cn.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfig
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/11/2 18:17
 * @Version 1.0
 */
@Configuration
/**
 * RedisTemplate工厂类(自定义序列化类型)
 */
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        //连接端口
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        //Jason序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer=new Jackson2JsonRedisSerializer(Object.class);
        //String序列化方式
        StringRedisSerializer stringRedisSerializer=new StringRedisSerializer();

        //键值均采用Jason序列化方式
   /*     redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer);*/
        //值采用Jason序列化方式
         redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
         redisTemplate.setKeySerializer(stringRedisSerializer);
        return redisTemplate;
    }
}
